//
//  SongListViewController.m
//  MMLOOK
//
//  Created by BraveMatch on 15/10/7.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "SongListViewController.h"
#import "SongListTableViewCell.h"
#import "AsynConnect.h"
#import "MyMusicModel.h"
#import "UIImageView+WebCache.h"
#import "PlayerViewController.h"
#import "MusicHandle.h"
#import "PlayerView.h"
#import "JudgeConnection.h"
#import "MBProgressHUD.h"
#define OffWidth [UIScreen mainScreen].bounds.size.width / 375
#define OffHeight [UIScreen mainScreen].bounds.size.height / 667
@interface SongListViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArr;
@property (nonatomic, retain) UIImageView *headerView;
@property (nonatomic, retain) MBProgressHUD *hud;

@end

@implementation SongListViewController
- (void)dealloc
{
    [_table release];
    [_tableArr release];
    [_imageUrl release];
    [_singerId release];
    [_hud release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArr = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self createSub];
    [self startConnection];

}

-(void)createSub
{
    UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.frame];
    [image setImage:[UIImage imageNamed:@"1.jpg"]];
    image.userInteractionEnabled = YES;
    [self.view addSubview:image];
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , self.view.frame.size.height - 64*OffHeight)];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.backgroundColor = [UIColor clearColor];
    self.table.delegate = self;
    self.table.dataSource = self;
    
    self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,220*OffHeight)];
    self.table.tableHeaderView = _headerView;
    
    [image addSubview:_table];
    
    
    [_table release];
    

}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableArr.count;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*OffHeight;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    PlayerViewController *playerVC = [[PlayerViewController alloc] init];
    
    
    
    [MusicHandle shareDan].imageUrl = self.imageUrl;
    MyMusicModel *model = [_tableArr objectAtIndex:indexPath.row];
    if (model.auditionList.count != 0) {
    NSMutableDictionary *dic = [model.auditionList objectAtIndex:0];
    NSString *url = [dic objectForKey:@"url"];
    [MusicHandle shareDan].urlSong = url;
    [MusicHandle shareDan].singname = model.name;
    [MusicHandle shareDan].singername = model.singerName;
    [MusicHandle shareDan].index = indexPath.row;
    [MusicHandle shareDan].urlarray = _tableArr;
    if (model.auditionList.count > 2) {
        NSMutableDictionary *dic = [model.auditionList objectAtIndex:1];
        NSString *url = [dic objectForKey:@"url"];
        [self playerMusicUrl:url];
    }else if (model.auditionList.count == 1){
        NSMutableDictionary *dic = [model.auditionList objectAtIndex:0];
        NSString *url = [dic objectForKey:@"url"];
        [self playerMusicUrl:url];
    }

    [self presentViewController:playerVC animated:YES completion:^{
    
        
    }];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"此歌曲暂时无法播放" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
    }
}


- (void)playerMusicUrl:(NSString *)url
{
    
    PlayerView *player = [PlayerView shareHandleController];
    
    
    [player play:url];
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"songList";
    SongListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[SongListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.backgroundColor = [UIColor clearColor];
    MyMusicModel *model = [_tableArr objectAtIndex:indexPath.row];
    [cell setValueWith:model];
    return cell;
    

}

-(void)startConnection
{
    [self createHud];
    
    //headerView图片
    NSString *headerUrl = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@?utdid=U%%2FXnNdXR1gwDAAnICPQYXIEM&detail=true",self.singerId];
    [AsynConnect asynConnectWithUrlstr:headerUrl parmater:nil block:^(NSData *data) {
        
        NSMutableDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *dic = [rootDic objectForKey:@"data"];
        
            [self.headerView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"picUrl"]] placeholderImage:nil];
        
        
        
    }];
    
    

    
    NSString *url = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50&app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=806c45eba2515bd74b0d6f00f64b96e461162b39&idfa=E7ECAA4B-8BA1-4BA8-9922-FFD011790A9F&utdid=VYVrXKwYuekDAGdTmMkz53%%2By&alf=201200&bundle_id=com.ttpod.music",self.singerId];
    [AsynConnect asynConnectWithUrlstr:url parmater:nil block:^(NSData *data) {
        
        NSMutableDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [rootDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in array) {
            MyMusicModel *model = [[MyMusicModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_tableArr addObject:model];
            [model release];
        }
        [_table reloadData];
        [_hud hide:YES];
    }];
    
}

-(void)createHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    self.hud.mode = MBProgressHUDModeIndeterminate;
    self.hud.labelText = @"加载中...";
    [self.hud show:YES];
    [self.view addSubview:_hud];
    [self.hud hide:YES afterDelay:5];
    [_hud release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
